import { GameData } from "./GameData";
import SettlementUI from "./SettlementUI";
import { EventTypeStr, SocketType } from "./SocketMag";
import SomeoneMenu from "./SomeoneMenu";

export class EventCtrl {

    public static Instance: EventCtrl = null;
    public static GetInst(): EventCtrl {
        if (!EventCtrl.Instance) {
            EventCtrl.Instance = new EventCtrl();
            EventCtrl.Instance.AddEvent();
        }
        return EventCtrl.Instance;
    }

    public AddEvent() {
        cc.director.on(EventTypeStr.setName, (data) => {
            cc.director.off(EventTypeStr.setName);
           
            GameData.Inst().m_nRoomid = data.roomid;
            GameData.Inst().m_vPlayerName = data.name;
            GameData.Inst().m_nCurRoomMasterid = GameData.Inst().m_nUserid = data.userid;
            SomeoneMenu.Inst.setRoomInfo(data.roomid, data.name);
            SomeoneMenu.Inst.m_PopView.destroy();
            SomeoneMenu.Inst.m_PopView = null
        }, this)

        cc.director.on(EventTypeStr.joinRoom, (data) => {
            GameData.Inst().m_nCurRoomMasterid = data.masterid;
            GameData.Inst().m_nRoomid = data.roomid;
            GameData.Inst().m_vPlayerName = data.name;
            SomeoneMenu.Inst.setRoomInfo(data.roomid, data.name);
            SomeoneMenu.Inst.Refresh();

            if (!SomeoneMenu.Inst.m_PopView) return
            SomeoneMenu.Inst.m_PopView.destroy();
            SomeoneMenu.Inst.m_PopView = null

        }, this)


        cc.director.on(EventTypeStr.returnSelfRoom, (data) => {

            GameData.Inst().m_nCurRoomMasterid = data.masterid;
            GameData.Inst().m_nRoomid = data.roomid;
            GameData.Inst().m_vPlayerName = data.name;
            SomeoneMenu.Inst.setRoomInfo(data.roomid, data.name);
            SomeoneMenu.Inst.Refresh();

        }, this)

        cc.director.on(EventTypeStr.reqHost, (data) => {

            GameData.Inst().m_nHostUserid = data.hostUserid;
            SomeoneMenu.Inst.setHostLab();
            SomeoneMenu.Inst.Refresh();

            if (GameData.Inst().m_nHostUserid == GameData.Inst().m_nUserid) {

                SomeoneMenu.Inst.OpenPopView(SocketType.wirteResule);
            }

        }, this)

        cc.director.on(EventTypeStr.wirteResule, (data) => {

            GameData.Inst().m_bstartGame = data.startgame;
            SomeoneMenu.Inst.Refresh();
            SomeoneMenu.Inst.DJsTime(data.gametime);

            if (!SomeoneMenu.Inst.m_PopView) return;
            SomeoneMenu.Inst.m_PopView.destroy();
            SomeoneMenu.Inst.m_PopView = null
        }, this)


        cc.director.on(EventTypeStr.game, (data) => {
            SomeoneMenu.Inst.NetDraw(data);
        }, this)


        cc.director.on(EventTypeStr.sendResule, (data) => {

            //某个玩家回答正确
            let userid = data.userid;
            let name = GameData.Inst().m_vPlayerName[userid];
            if (userid == GameData.Inst().m_nUserid) {
                SomeoneMenu.Inst.EditBox_resule.node.active = false;
                SomeoneMenu.Inst.btn_send.active = false;
            }

            let boo = SomeoneMenu.Inst.m_brunText
            if (boo) {
                SomeoneMenu.Inst.m_vSuccName.push(name);
            } else {
                SomeoneMenu.Inst.runText(name);
            }

        }, this)

        //结算界面
        cc.director.on(EventTypeStr.gameend, (data) => {
            GameData.Inst().m_nHostUserid = 0;
            SomeoneMenu.Inst.StopDJs();
            cc.resources.load('Settlement', cc.Prefab, (error, prefab: cc.Prefab) => {
                let settlementUI = cc.instantiate(prefab);
                SomeoneMenu.Inst.node.addChild(settlementUI);
                (settlementUI.getComponent('SettlementUI') as SettlementUI).OpenListInfo(data);
            })
        }, this)


        //准备
        cc.director.on(EventTypeStr.readyGame, (data) => {

            GameData.Inst().m_nCurRoomMasterid = data.masterid;
            GameData.Inst().m_nRoomid = data.roomid;
            GameData.Inst().m_vPlayerName = data.name;
            SomeoneMenu.Inst.setRoomInfo(data.roomid, data.name);
            SomeoneMenu.Inst.Refresh();
            SomeoneMenu.Inst.ReadyGame();
            
        }, this)
    }
}

